package com.item.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.item.dao.DepartmentMapper;
import com.item.dao.model.Department;
import com.item.dao.model.DepartmentExample;

@Service
public class DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    private static final Logger logger = Logger.getLogger(DepartmentService.class);

    public int countByExample(DepartmentExample example) {
        return this.departmentMapper.countByExample(example);
    }

    public Department selectByPrimaryKey(String id) {
        return this.departmentMapper.selectByPrimaryKey(id);
    }

    public List<Department> selectByExample(DepartmentExample example) {
        return this.departmentMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String id) {
        return this.departmentMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Department record) {
        return this.departmentMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Department record) {
        return this.departmentMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(DepartmentExample example) {
        return this.departmentMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Department record, DepartmentExample example) {
        return this.departmentMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Department record, DepartmentExample example) {
        return this.departmentMapper.updateByExample(record, example);
    }

    public int insert(Department record) {
        return this.departmentMapper.insert(record);
    }

    public int insertSelective(Department record) {
        return this.departmentMapper.insertSelective(record);
    }
    
    public String getLeastCode(String pid) {
    	String res = "D";
    	DepartmentExample example = new DepartmentExample();
    	example.createCriteria().andPidEqualTo(pid);
    	example.setOrderByClause("code ASC");
    	List<Department> list = this.departmentMapper.selectByExample(example);
    	int i = 0;
    	for(int len = list.size(); i < 100 && i < len; i++) {
    		Department s = list.get(i);
    		int sCodeInt = Integer.parseInt(s.getCode().substring(1));
    		if(i < sCodeInt) {
    			break;
    		}
    	}
    	if(i == 0)
    		res += "01";
    	else
    		res += i < 10 ? "0" + i : i + "";
    	
    	return res;
    }
}